package com.hnhcyy.goods.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hnhcyy.goods.mapper.SpkfkDescMapper;
import com.hnhcyy.goods.pojo.SpkfkDesc;
import com.hnhcyy.goods.service.SpkfkDescAdminService;

/**
 * 商品详情service实现类 提供后台使用
 * @author Administrator
 */
@Service
public class SpkfkDescAdminServiceImpl implements SpkfkDescAdminService {

	@Autowired
	SpkfkDescMapper spkfkDescMapper;

	public static final ObjectMapper MAPPER = new ObjectMapper();

	/**
	 * 获取商品详情
	 */
	@Override
	public String selectGoodsDetail(String spid) {

		SpkfkDesc goodsDetail = spkfkDescMapper.selectItemBySpid(spid);

		// 转化为json字符串返回，因为admin端调用用的是原生feign，直接返回List和Mapper会提示解码错误
		String result = "";
		try {
			result = MAPPER.writeValueAsString(goodsDetail);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}
	
	/**
	 * 更新商品详情
	 */
	@Override
	public String updateGoodsDetail(SpkfkDesc goodsDetail) {
		
		System.out.println("-----> goodsDetail" + goodsDetail.getItemDesc());
		
		//获取spid
		String spid = goodsDetail.getSpid();
		//查询是否已经存在该商品详情记录
		SpkfkDesc oldDetail = spkfkDescMapper.selectItemBySpid(spid);
		
		int result = 0;
		
		if (null == oldDetail){
			//不存在记录，则新增
			result = spkfkDescMapper.insertItem(goodsDetail);
		}else {
			//已经存在商品详情记录，则更新商品商品详情记录
			result = spkfkDescMapper.updateItem(goodsDetail);
		}
		
		return result + "";
	}

}
